﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace autoBidProject
{
    public partial class Map : Form
    {
        public Map()
        {
            InitializeComponent();
        }

        private void Map_Load(object sender, EventArgs e)
        {
            LoadTreeview();
            LoadGridview();
        }

        private void btMap_Click(object sender, EventArgs e)
        {
            string ibetMatch = tvIBET.SelectedNode.Text;
            string sboMatch = tvSBO.SelectedNode.Text;

            MatchManager eventManager = (MatchManager)this.Tag;
            eventManager.Map(ibetMatch, sboMatch);
            LoadTreeview();
            LoadGridview();
            this.Tag = eventManager;
        }

        private void btAutoMap_Click(object sender, EventArgs e)
        {
            MatchManager eventManager = (MatchManager)this.Tag;
            eventManager.AutoMap();
            LoadTreeview();
            LoadGridview();
            this.Tag = eventManager;
        }

        private void LoadTreeview()
        {
            MatchManager eventManager = (MatchManager)this.Tag;
            tvSBO.Nodes.Clear();
            tvIBET.Nodes.Clear();

            if(eventManager.IbetLeagues != null)
                AddToTree(ref tvIBET, eventManager.IbetLeagues);

            if (eventManager.SboLeagues != null)
                AddToTree(ref tvSBO, eventManager.SboLeagues);

            tvIBET.ExpandAll();
            tvSBO.ExpandAll();
        }

        private void AddToTree(ref TreeView tree, List<League> leagues)
        {
            foreach (League league in leagues)
            {
                TreeNode node = new TreeNode();
                node.Text = league.Name;
                foreach (Match e in league.Matchs)
                {
                    if (!IsMapped(e.Name()))
                    {
                        TreeNode child = new TreeNode();
                        child.Text = e.Name();
                        node.Nodes.Add(child);
                    }
                }
                if(node.Nodes.Count > 0)
                    tree.Nodes.Add(node);
            }
        }

        private bool IsMapped(string ibetName)
        {
            MatchManager eventManager = (MatchManager)this.Tag;
            List<string[]> mapped = eventManager.Mapper;
            for (int i = 0; i < mapped.Count; i++)
                if (ibetName.CompareTo(mapped[i][0]) == 0)
                    return true;
            return false;
        }

        private void LoadGridview()
        {
            MatchManager eventManager = (MatchManager)this.Tag;
            dgMapped.Rows.Clear();

            List<string[]> mapper = eventManager.Mapper;
            for (int i = 0; i < mapper.Count; i++)
            {
                string[] item = new string[] {(i+1).ToString(), mapper[i][0], mapper[i][1]};
                dgMapped.Rows.Add(item);
            }
        }

        private void btDeleteAll_Click(object sender, EventArgs e)
        {
            MatchManager eventManager = (MatchManager)this.Tag;
            eventManager.CleanMapper();
            LoadTreeview();
            LoadGridview();
            this.Tag = eventManager;
        }

        private void btDeleteOneMapped_Click(object sender, EventArgs e)
        {
            MatchManager eventManager = (MatchManager)this.Tag;
            if(dgMapped.CurrentRow != null)
            {
                string match = (string)dgMapped.CurrentRow.Cells["IbetMatchs"].Value;
                eventManager.RemoveMap(match);
                LoadTreeview();
                LoadGridview();
            }
            this.Tag = eventManager;
        }

        
    }
}
